/**
 * handle controller device leave.
 */
var cmdCodes = require("../CmdCode/cmdCode.js");
var gatewayTable = require("../Redis/gatewayTable.js");
var redis = require("../Redis/redis.js");
var controllerTable = require("../Redis/controllerTable.js");

function leaveHandler(cmd, c) {
	"use strict";
	var devId = cmd.devId;
	var gwId = c.gwId;
	
	var context = {};
	redis.setClient(context);
	
	context.beginMulti();
	
	gatewayTable.delController(context, gwId, devId);
	controllerTable.setFields(context, devId, ["online", "false"]);
	
	context.endMulti(function(err){
		if (err) {
			console.error("leave handler database error:" + err);
		}
	});
}

leaveHandler.cmdCode = cmdCodes.CMD_CONTROLLER_LEAVE;
module.exports = leaveHandler;
